package servlets;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import beans.AtuacaoProfissional;
import beans.Endereco;
import beans.FormacaoAcademica;
import beans.FormacaoComplementar;
import beans.Informacoes;
import beans.ProducaoBibliografica;
import beans.Usuario;


public class RecuperaDadosServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    public RecuperaDadosServlet() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processaCurriculo(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processaCurriculo(request, response);
	}
	
	
	public void processaCurriculo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		request.setCharacterEncoding("UTF-8");
		
		String buscaEmail = request.getParameter("buscaEmail");
		String url = "curriculo.jsp";
		HttpSession session = request.getSession();
		
		if(buscaEmail != null && !buscaEmail.equals("")) {
			String path = getServletContext().getRealPath("/bancodados/"+buscaEmail);
			FileInputStream fis;
			ObjectInputStream ois;			
			try{				
				/*Carrega informacoes*/
				fis = new FileInputStream(path+"_"+"informacoes.obj");
				ois = new ObjectInputStream(fis);
				
				Informacoes informacoes = (Informacoes)ois.readObject();
				ois.close();
				session.removeAttribute("busca_informacoes");
				session.setAttribute("busca_informacoes", informacoes);
				
				/*Carrega endereco*/
				fis = new FileInputStream(path+"_"+"endereco.obj");
				ois = new ObjectInputStream(fis);
				
				Endereco endereco = (Endereco)ois.readObject();
				ois.close();
				session.removeAttribute("busca_endereco");
				session.setAttribute("busca_endereco", endereco);
			} catch(Exception ex) {			
				url = "usuarioSemDados.jsp";
			}
			
			try {
				/*Carrega Formações academicas*/				
				fis = new FileInputStream(path+"_"+"formacao_academica.obj");
				ois = new ObjectInputStream(fis);
				ArrayList<FormacaoAcademica> formAcadLista = (ArrayList<FormacaoAcademica>)ois.readObject();
				ois.close();
				session.removeAttribute("busca_formAcadLista");
				session.setAttribute("busca_formAcadLista", formAcadLista);				
			} catch(Exception ex) {
				//JOptionPane.showConfirmDialog(null, ex.getMessage());
			}
				
			try {
				/*Carrega Formações complementares*/				
				fis = new FileInputStream(path+"_"+"formacao_complementar.obj");
				ois = new ObjectInputStream(fis);
				ArrayList<FormacaoComplementar> formCompLista = (ArrayList<FormacaoComplementar>)ois.readObject();
				
				ois.close();
				session.removeAttribute("busca_formCompLista");
				session.setAttribute("busca_formCompLista", formCompLista);				
			}catch(Exception ex) {
				//JOptionPane.showConfirmDialog(null, ex.getMessage());
			}
				
			try {
				/*Carrega Atuações profissionais*/			
				fis = new FileInputStream(path+"_"+"atuacao_profissional.obj");
				ois = new ObjectInputStream(fis);
				ArrayList<AtuacaoProfissional> atuaProfLista = (ArrayList<AtuacaoProfissional>)ois.readObject();
					
				ois.close();
				session.removeAttribute("busca_atuaProfLista");
				session.setAttribute("busca_atuaProfLista", atuaProfLista);				
			}catch(Exception ex) {
				//JOptionPane.showConfirmDialog(null, ex.getMessage());
			}
				
			try {
				/*Carrega Produções bibliográficas*/		
				fis = new FileInputStream(path+"_"+"producao_bibliografica.obj");
				ois = new ObjectInputStream(fis);
				ArrayList<ProducaoBibliografica> prodBiblioLista = (ArrayList<ProducaoBibliografica>)ois.readObject();
				
				ois.close();
				session.removeAttribute("busca_prodBiblioLista");
				session.setAttribute("busca_prodBiblioLista", prodBiblioLista);				
			}catch(Exception ex) {
				//JOptionPane.showConfirmDialog(null, ex.getMessage());
			}
			
			RequestDispatcher dispatcher = request.getRequestDispatcher(url);
			dispatcher.forward(request, response);
			
			return;
		}
		
		url = "curriculo.jsp?acao=busca";
		
		response.setContentType("text/html;charset=UTF-8");  
		
		RequestDispatcher dispatcher = request.getRequestDispatcher(url);
		dispatcher.forward(request, response);
		
	}

}
